/*
 * Copyright 2013 Glassy Cinema.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hu.glassy.web.panel.header.login;

import hu.glassy.core.model.Account;
import hu.glassy.web.auth.ApplicationSession;
import hu.glassy.web.component.common.CustomAjaxSubmitLink;
import hu.glassy.web.component.feedback.CustomFeedbackPanel;
import hu.glassy.web.component.visitor.ErrorMessageFormVisitor;
import hu.glassy.web.page.account.registration.AccountActivationPage;
import hu.glassy.web.page.home.UserHomePage;
import hu.glassy.web.util.PasswordHelper;
import org.apache.wicket.Component;
import org.apache.wicket.Session;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.feedback.ComponentFeedbackMessageFilter;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;

/**
 *
 * @author molnaralexander
 */
public final class LoginFormPanel extends Panel
{

  // E-mail address
  private String email;
  // Password
  private String password;
  // The form
  private final Form<Account> loginForm;

  public LoginFormPanel(String id)
  {
    super(id);
    setDefaultModel(new CompoundPropertyModel<LoginFormPanel>(this));

    loginForm = new Form("loginForm");
    loginForm.add(new CustomFeedbackPanel("loginFeedback").setFilter(new ComponentFeedbackMessageFilter(loginForm)));
    loginForm.add(new TextField<String>("email").setRequired(true));
    loginForm.add(new PasswordTextField("password").setRequired(true));
    loginForm.add(generateLoginLink());
    loginForm.visitChildren(new ErrorMessageFormVisitor<String>());
    add(loginForm);
  }

  private Component generateLoginLink()
  {
    return new CustomAjaxSubmitLink("loginLink")
    {
      private static final long serialVersionUID = 1L;

      @Override
      protected void onSubmit(final AjaxRequestTarget target, final Form<?> form)
      {
        super.onSubmit(target, form);

        login(email, PasswordHelper.encrypt(password), target);
      }
    };
  }

  private void login(final String email, final String password, final AjaxRequestTarget target)
  {
    if (AuthenticatedWebSession.get().signIn(email, password))
    {
      if (ApplicationSession.get().getAccount().getObject().isActive())
      {
        setResponsePage(UserHomePage.class);
      }
      else
      {
        Session.get().error(getString("login.activation.required"));
        setResponsePage(AccountActivationPage.class);
      }

    }
    else
    {
      loginForm.error(getString("login.loginerror"));
    }
  }
}
